//
//  HEFillAccusationViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/4.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEFillAccusationViewController.h"

@interface HEFillAccusationViewController ()<QMUITextViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIImageView *userIcon;

@property (nonatomic, strong) UILabel *usernameLb;

@property (nonatomic, strong) QMUIGhostButton *selectBtn;
 
@property (nonatomic, strong) QMUITextView *textView;

@end

@implementation HEFillAccusationViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"举报";
     
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
    
    [self.contentView addSubview:self.userIcon];
    [self.contentView addSubview:self.usernameLb];
    
    [self.userIcon sd_setImageWithURL:[NSURL URLWithString:_user.avatarUrlString] placeholderImage:IMAGE_NAMED(@"defualt_icon")];

    self.usernameLb.text = _user.showName;
    
    
    // line
    UIView *line = [UIView new];
    line.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:line];
    
    // 描述
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    _textView.placeholder = @"请输入举报原因";
    _textView.font = DYFont(17);
    self.textView.maximumTextLength = 15000;
    self.textView.placeholderColor = CNavDargGrayColor; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: DYFont(17),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    _textView.delegate = self;
    self.textView.layer.cornerRadius = 4;
    [self.contentView addSubview:_textView];
    
    
    // 原因
    UILabel *_resLb = [UILabel new];
    _resLb.text = @"举报原因";
    _resLb.font = DYBlodFont(17);
    _resLb.textColor = CNavLightBlackColor;
    [_contentView addSubview: _resLb];
    
 
    // 提交
    QMUIFillButton *submitBtn = [[QMUIFillButton alloc] initWithFillColor:CPriceColor titleTextColor:CNavBgFontColor];
    submitBtn.titleLabel.font = UIFontMake(16);
    [submitBtn setTitle:@"举报" forState:UIControlStateNormal];
    [_contentView addSubview:submitBtn];
    [submitBtn addTarget:self action:@selector(submitApply) forControlEvents:UIControlEventTouchUpInside];

    
    [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.width.height.offset(80);
        make.left.equalTo(_contentView).offset(15);
    }];
    
    [self.usernameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.userIcon);
        make.left.equalTo(self.userIcon.mas_right).offset(15);
        make.right.equalTo(self.contentView).offset(15);
    }];
    
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userIcon.mas_bottom).mas_offset(15);
        make.left.equalTo(_contentView);
        make.right.equalTo(_contentView);
        make.height.offset(.5);
    }];
    
    [_resLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).mas_offset(15);
        make.left.equalTo(_contentView).mas_offset(15);
    }];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).mas_offset(10);
        make.left.equalTo(_resLb.mas_right).mas_offset(15);
        make.right.equalTo(_contentView).mas_offset(-15);
        make.height.offset(180);
    }];

    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.left.equalTo(_contentView).mas_offset(40);
        make.right.equalTo(_contentView).mas_offset(-40);
        make.height.mas_offset(40);
        make.bottom.equalTo(_contentView).mas_offset(-50);
    }];
}

- (void)submitApply {
 
    if ([DYTools isBlankString: self.textView.text]) {
        [Dialog toastBottom:@"请填写举报原因!"];
        return;
    }
    
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:_roomId forKey:@"roomId"];
    [dict setValue:_user.infoId forKey:@"accusationUsid"];
    [dict setValue:_user.showName forKey:@"accusationRealname"];
    [dict setValue:self.textView.text forKey:@"accusationContent"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KRoomSaveAccusation parameters:dict success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];

        if (response.success) {
            [Dialog toastBottom:@"已提交举报"];
            if (self.backBlock) {
                self.backBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
 

#pragma mark -
- (UIImageView *)userIcon {
    if (!_userIcon) {
        _userIcon = [UIImageView new];
        _userIcon.layer.masksToBounds = YES;
        _userIcon.layer.cornerRadius = 40;
    }
    return _userIcon;
}

- (UILabel *)usernameLb {
    if (!_usernameLb) {
        _usernameLb = [UILabel new];
        _usernameLb.font = DYFont(16);
    }
    return _usernameLb;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
//        make.height.greaterThanOrEqualTo(@0.0f);
        make.height.mas_greaterThanOrEqualTo(kScreenHeight-ZXNavBarHeight);
    }];
}

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
        _scrollView.alwaysBounceVertical = YES;
        _scrollView.bounces = YES;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

@end
